using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
        cn.Open();
        oleDbCmd.CommandText =
        "SELECT     Customers.Address, "+
        "  Customers.City,             "+
        "   Customers.Region,          "+ 
        "   Customers.PostalCode,      "+
        "   Customers.Country,         "+
        "   Customers.CompanyName,     "+
        "   Orders.OrderID,            "+
        "   Orders.CustomerID,         "+
        "   Orders.EmployeeID,         "+
        "   Orders.OrderDate,          "+
        "   Orders.RequiredDate,       "+
        "   Orders.ShippedDate,        "+
        "   Orders.ShipVia,            "+
        "   Shippers.CompanyName AS Shp_CompanyName, "+
        "   Employees.LastName,        "+
        "   Employees.FirstName,       "+
        "   Products.ProductName,      "+
        "   [Order Details].ProductID, "+
        "   [Order Details].UnitPrice, "+
        "   [Order Details].Quantity,  "+
        "   [Order Details].Discount,  "+
        "   Orders.Freight,            "+
        "   Orders.ShipName,           "+
        "   Orders.ShipAddress,        "+ 
        "   Orders.ShipCity,           "+
        "   Orders.ShipRegion,         "+
        "   Orders.ShipPostalCode,     "+
        "   Orders.ShipCountry         "+
        " FROM         (((((Orders INNER JOIN "+
        "              Customers ON Orders.CustomerID = Customers.CustomerID) INNER JOIN "+
        "              Shippers ON Orders.ShipVia = Shippers.ShipperID) INNER JOIN "+
        "              [Order Details] ON Orders.OrderID = [Order Details].OrderID) INNER JOIN "+
        "              Employees ON Orders.EmployeeID = Employees.EmployeeID) INNER JOIN "+
        "              Products ON [Order Details].ProductID = Products.ProductID) "+
        "  WHERE     (Orders.OrderID = @OrderID) " +
        "   ORDER BY Orders.CustomerID, Orders.OrderID";

            oleDbCmd.Parameters.Clear();
            oleDbCmd.Parameters.Add("OrderID", OleDbType.Integer);
            Int32 OrderID;
            OrderID = (Int32)((DataRowView)BindingContext[dataGridView2.DataSource].Current)["OrderID"];
            oleDbCmd.Parameters["OrderID"].Value = OrderID;
            OleDbDataReader reader = oleDbCmd.ExecuteReader();
            reader.Read();

            ekRtf1.ReportData.Add(reader, "Qr"); //Attach reader to the report with alias "Qr"
            ekRtf1.OutFile = "Order_" + OrderID.ToString()+".doc";
            ekRtf1.ExecuteOpen();
            ekRtf1.ReportData.Clear();

            reader.Close();
            cn.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cmd1.CommandText = "SELECT     CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax " +
                               "FROM   Customers";
            adp1.Fill(dataSet1, "Customers");
            dataGridView1.DataSource = dataSet1.Tables["Customers"].DefaultView;

            
            cmd2.CommandText = "SELECT     OrderID, CustomerID, OrderDate, RequiredDate, ShipName, ShipAddress " +
                             "FROM Orders WHERE     (CustomerID = @CustomerID)";
            cmd2.Parameters.Add("CustomerID", OleDbType.Char);
            cmd2.Parameters["CustomerID"].Value = "";
            adp2.Fill(dataSet1, "Orders");
            dataGridView2.DataSource = dataSet1.Tables["Orders"].DefaultView;
            this.BindingContext[dataGridView1.DataSource].PositionChanged +=
                customersBindingSource_CurrentChanged;
            customersBindingSource_CurrentChanged(this,new EventArgs());
        }

        private void customersBindingSource_CurrentChanged(object sender, EventArgs e)
        {

            string s;
            s = ((DataRowView)BindingContext[dataGridView1.DataSource].Current)["CustomerID"].ToString();
            cmd2.Parameters["CustomerID"].Value = s;
            dataSet1.Tables["Orders"].Rows.Clear();
            adp2.Fill(dataSet1, "Orders");
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}